package com.example.other.controller;

import cn.hutool.core.util.StrUtil;
import com.example.other.dto.file.GenFileDto;
import com.example.other.dto.file.TempKv;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @description：TODO
 * @author：yufan.wang
 * @create：2023/7/18 20:42
 */
@CrossOrigin
@Controller
@RequestMapping
public class IndexController {

    @GetMapping({"/index","/"})
    public String toIndex(Model model){
        model.addAttribute("uid","123456789");
        return "index";
    }

    @PostMapping({"/genFile"})
    @ResponseBody
    public String genFile(@RequestBody GenFileDto dto){
        String format = templateReplace(dto);
        return format;
    }

    private String templateReplace(GenFileDto dto) {
        Map<String, String> map = dto.getTempParams().stream().collect(Collectors.toMap(TempKv::getTemK, TempKv::getTemV));
        String format = StrUtil.format(dto.getSoucre(), map, true);
        return format;
    }


}
